<?php class News_m extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
          $this->load->database();
    }

    function get_news($per_page, $off_set)
    {
         $this->db->select('news.*,cat_news.title as cat_news_title');
        $this->db->from('news');
         $this->db->join('cat_news','news.catid=cat_news.id','left');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    function get_detail_news($id)
    {   $this->db->select('news.*,cat_news.title as cat_news_title');
        $this->db->from('news');
        $this->db->join('cat_news','news.catid=cat_news.id');
        $this->db->where(array('news.id' => $id));
        $query = $this->db->get();
        return $query->row_array();
    }
    function count_news()
    {
         $this->db->select('news.*,cat_news.title as cat_news_title');
        $this->db->from('news');
         $this->db->join('cat_news','news.catid=cat_news.id');
        $this->db->order_by('news.id', 'DESC');
        $query = $this->db->get();
        return $query->num_rows();
    }
    function update_news($data, $id){
         return $this->db->update('news',$data,array('id'=>$id));
    }
     function add_news($data){
          return $this->db->insert('news',$data);
      }
     function delete_news($id){
       return  $this->db->delete('news', array('id' => $id));
    }
    function delete_multi_news($list){
      $this->db->where('id in '.$list);
      $query=  $this->db->delete('news');
      return $query;
    }

     function get_category()
    {  $query = $this->db->get('cat_news');
        return $query->result_array();
    }

     function get_cat_limit($per_page, $off_set)
    {
         $this->db->select();
        $this->db->from('cat_news');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('id', 'ASC');
        $query = $this->db->get();
        return $query->result_array();
    }
    function add_cat($data){
          return $this->db->insert('cat_news',$data);
      }
       function update_cat($data, $id){
         return $this->db->update('cat_news',$data,array('id'=>$id));
    }
    function get_detail_cat($id)
    {
        $query=$this->db->get_where('cat_news', array('id' => $id));
        return $query->row_array();
    }

       function delete_cat($id){
       return  $this->db->delete('cat_news', array('id' => $id));
    }
    function delete_multi_cat($list){
      $this->db->where('id in '.$list);
      $query=  $this->db->delete('cat_news');
      return $query;
    }
}
?>
